package com.eb.bi.common;

import com.eb.bi.common.util.CommonProcessor;
import com.eb.bi.common.util.DateProcessor;
import com.eb.bi.common.util.ENVProcessor;
import com.eb.bi.common.util.FileProcessor;
import com.eb.bi.common.util.LoggerProcessor;
import com.eb.bi.common.PluginResult;
public class PluginUtil {
	private static PluginUtil PluginUtil = null;
	
	private String m_repPath = "";
	
	//����JAVA�����本程序的名称xxx.jarԼ��ĳ�����ƣ�������ʱ�ò���
	private String m_name ="";
	
	private PluginParam PluginParam = new PluginParam();
	private PluginConfig PluginConfig = new PluginConfig();
	
	private String m_rootPath = "";
	
	private String m_jobId;
	private String m_taskId;
	private String m_batchId;
	private String m_baseTime;
	
	private String m_metaDataPath;
	private String m_runBatchPath;
	private String m_logFilePath;
	private String m_configFilePath;
	private String m_resultFilePath;
	private  PluginUtil(){
		m_rootPath = getenv("IDOXDIR");
		//System.out.println(m_rootPath);
	}
	
	private static PluginUtil pluginUtilInit(){
		if(PluginUtil == null){
			PluginUtil = new PluginUtil();
		}
		return PluginUtil;
	}
	
	private String getenv(String idoxPath){
		try {
			ENVProcessor ENVProcessor = new ENVProcessor();
			return ENVProcessor.getEnv().getProperty(idoxPath);
		} catch (Exception e) {
			e.printStackTrace();
			return "Get env idox properties failed!";
		}
	}
	
	public static PluginUtil getInstence(){
		return pluginUtilInit();
	}
	
	public  void init(String[] args){
		initRepPath();
		initResource();
		initParam(args);
		initLog();
		initDBLog();
		initConfig();
		initResult();
	}
	
	private void initRepPath(){
		FileProcessor FileProcessor = new FileProcessor();
		StringBuffer stringBuffer = FileProcessor.getTotalFileInfo(m_rootPath+"/etc/config.idox");
		if(stringBuffer != null){
			int sPos = stringBuffer.indexOf("<savePath");
			if(sPos != -1){
				String temp = stringBuffer.substring(sPos);
				int lPos = temp.indexOf("value=");
				if(lPos != -1){
					temp = temp.substring(lPos + 7);
					if(temp.indexOf("\"") != 0){
						this.m_repPath = temp.substring(0,temp.indexOf("\""));
						return;
					}
				}
			}
			
		}
		this.m_repPath = this.m_rootPath + "/repository";
		System.out.println("RootPath: "+this.m_rootPath+"  RepositoryPath: "+this.m_repPath);
	}
	
	public void initResource(){
		
	}
	
	public void initParam(String[] args){
		CommonProcessor CommonUtil = new CommonProcessor();
		m_name = CommonUtil.getJarFileName();
		
		m_baseTime = new DateProcessor().getNowFullTime();
		
		PluginParam.addCheckedParam("-job_id");
		PluginParam.addCheckedParam("-task_id");
		PluginParam.addCheckedParam("-batch_id");
		if(!this.PluginParam.parse(args)){
			System.err.println("Parse input params failed."+PluginParam.getM_errorDesc());
			System.exit(PluginExitCode.PE_ARG_ERR);
		}
		PluginParam.print();
		m_jobId = PluginParam.getM_params().get("-job_id");
		m_taskId = PluginParam.getM_params().get("-task_id");
		m_batchId = PluginParam.getM_params().get("-batch_id");
		if(PluginParam.paramExists("-base_time")){
			m_baseTime = PluginParam.getM_params().get("-base_time");
		}
		m_metaDataPath = m_repPath + "/" + m_jobId + "/meta_data";
		m_runBatchPath = m_repPath + "/" + m_jobId + "/running_batch/"  + m_batchId;
		System.out.println("jobId: " + m_jobId
				+ " batchId: " + m_batchId
				+ " taskId: " + m_taskId
				+ " baseTime: " + m_baseTime
				+ " metaDataPath: " + m_metaDataPath
				+ " runBatchPath: " + m_runBatchPath
				);
	}
	
	public boolean initLog(){
		String logCategory = this.PluginParam.getParam("-log_category");
		String logConfigFile = this.m_rootPath + "/etc/log4j.properties";
		String category = (logCategory!=null && !"".equals(logCategory) ?  logCategory : "default" );
		System.out.println("log category ="+ category);
		m_logFilePath = m_runBatchPath +"/"+ m_name+".task" + m_taskId + ".log";
		System.out.println("log path:  "+m_logFilePath);
		if(!LoggerProcessor.getLogInstance().startup(logConfigFile, category, m_logFilePath)){
			System.err.println("Open log file "+ m_logFilePath +" failed.[Reason: "
					+LoggerProcessor.getLogInstance().getM_errorDesc()+" ]");
			System.exit(PluginExitCode.PE_LOG_ERR);
		}
		return true;
	}
	
	public boolean initDBLog(){
		return true;
	}
	
	public void initConfig(){
		m_name = "tt";
		m_configFilePath = m_metaDataPath+"/"+m_name+".task"+m_taskId+".config";
		
		if(!PluginConfig.load(m_configFilePath)){
			System.err.println("Load config file ("+m_configFilePath+") failed.[Reason: "+PluginConfig.getM_errorDesc()+"]");
	
			System.exit(PluginExitCode.PE_CONFIG_ERR);
		}
		PluginConfig.print();
	}
	
	public void initResult(){
		
		
		m_resultFilePath = m_runBatchPath + "/" + m_name + ".task"+m_taskId+".result";
		PluginResult.getInstance().setFilePath(m_resultFilePath);
		DateProcessor DateProcessor = new DateProcessor();
		PluginResult.getInstance().setBeginTime(DateProcessor.getNowFullTime());
		PluginResult.getInstance().setJobId(m_jobId);
		PluginResult.getInstance().setBatchId(m_batchId);
		PluginResult.getInstance().setTaskId(m_taskId);
		PluginResult.getInstance().save();
	}
	
	public static void main(String[] args){
		PluginUtil.getInstence().init(args);
		
	}

	public PluginConfig getPluginConfig() {
		return PluginConfig;
	}
}
